package com.wcl.hencodertest.test13_multi_touch;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.wcl.hencodertest.Util;

public class MultiTouchView extends View {

    public static final int IMAGE_WIDTH = ConvertUtils.dp2px(300);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Bitmap bitmap;
    private float offsetX;
    private float offsetY;
    private float downX;
    private float downY;
    private float originalOffsetX;
    private float originalOffsetY;
    private int trackingPointerId;

    public MultiTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        bitmap = Util.getAvatar(IMAGE_WIDTH);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, offsetX, offsetY, paint);
    }


    /**
     * 手指的ID不变,但是index会变,所以需要通过记录ID,在使用的时候,通过ID寻找index,event.getX(index);
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                trackingPointerId = event.getPointerId(0);//记录当前需要跟踪的手指ID,当前ACTION_DOWN,index肯定是0
                downX = event.getX();//记录手指点击按下时候的偏移量,用在后续减掉
                downY = event.getY();
                originalOffsetX = offsetX;//记录上次图片移动的位置
                originalOffsetY = offsetY;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                int actionIndex = event.getActionIndex();//当前按下的手指的index
                trackingPointerId = event.getPointerId(actionIndex);//通过index记录track的手指ID
                downX = event.getX(actionIndex);//记录手指点击按下时候的偏移量,用在后续减掉
                downY = event.getY(actionIndex);
                originalOffsetX = offsetX;//记录上次图片移动的位置
                originalOffsetY = offsetY;
                break;
            case MotionEvent.ACTION_MOVE:
                int pointerIndex = event.findPointerIndex(trackingPointerId);//通过记录下的trackingPointerId,拿到当前应该处理的手指的index
                offsetX = event.getX(pointerIndex) - downX + originalOffsetX;
                offsetY = event.getY(pointerIndex) - downY + originalOffsetY;
                invalidate();
                break;
            case MotionEvent.ACTION_POINTER_UP:
                int index = event.getActionIndex();//拿到抬起手指的index
                int pointerId = event.getPointerId(index);//通过index获取pointerId
                if (pointerId == trackingPointerId) {//如果Id是当前正在控制移动的手指,那么才处理
                    int newIndex;
                    if (index == event.getPointerCount() - 1) {//如果当前抬起的手指,是最后一根手指,那么交给的处理事件手指的getPointerCount-2
                        newIndex = event.getPointerCount() - 2;
                    } else {
                        newIndex = event.getPointerCount() - 1;//如果当前抬起的手指不是最后一根手指,那么交给的处理事件手指的getPointerCount-1
                    }
                    trackingPointerId = event.getPointerId(newIndex);
                    downX = event.getX(newIndex);//记录交付给的手指的当前位置,用在后续减掉
                    downY = event.getY(newIndex);
                    originalOffsetX = offsetX;//记录当前的偏移量
                    originalOffsetY = offsetY;
                }

                break;
        }

        return true;
    }
}
